package ar.uba.dc.tl.mylanga.outputgenerator;

import ar.uba.dc.tl.mylanga.interpreter.Point;

import java.io.*;
import java.nio.charset.Charset;
import java.util.List;

public class CsvOutputGenerator {

	private List<Point> points;

	public CsvOutputGenerator(List<Point> points) {
		assert points != null;

		this.points = points;
	}

	public void generate(String filename) {
		Writer writer = null;

		try {
			writer = new OutputStreamWriter(new FileOutputStream(filename), Charset.forName("UTF-8").newEncoder());

			this.generate(writer);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void generate(Writer writer) throws IOException {
		for (Point point : this.points) {
			writer.write(Double.toString(point.getX()));
			writer.write(", ");
			writer.write(Double.toString(point.getY()));
			writer.write(System.lineSeparator());
		}

		writer.flush();
	}
}
